home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / SUMBLOCK.AML < prev    next >
Text File  |  1996-07-17  |  3KB  |  111 lines

  1. //--------------------------------------------------------------------
  2. // SUMBLOCK.AML
  3. // Add Numbers in a Block, (C) 1993-1996 by nuText Systems
  4. //
  5. // This macro adds up numbers in a marked block in the current edit
  6. // window and displays the total. Sumblock will handle numbers from
  7. // -2 billion to +2 billion with a maximum of 8 decimal places.
  8. //
  9. // Usage:
  10. //
  11. // Select this macro from the Macro List (on the Macro menu), or run it
  12. // from the macro picklist <shift f12>.
  13. //--------------------------------------------------------------------
  14.  
  15. // compile time macros and function definitions
  16. include  bootpath "define.aml"
  17.  
  18. variable width, frac_total, int_total, count
  19.  
  20. // do only if a mark exists in current window
  21. if getcurrbuf <> getmarkbuf then
  22.   msgbox "Block not marked in current window"
  23.   return
  24. end
  25.  
  26. // close mark and save cursor position
  27. stopmark
  28. pushcursor
  29.  
  30. loop
  31.  
  32.   // find numbers in the mark using regular expressions
  33.   width = find "-?{[0-9]*\\.[0-9]#}|{[0-9]#}"
  34.                (if? width "xb*" "xbg*")
  35.   if width then
  36.  
  37.     // count numbers found
  38.     count = count + 1
  39.  
  40.     // get number and position of decimal character
  41.     number = gettext (getcol) width
  42.     p = pos '.' number
  43.  
  44.     // handle fractional portion of number
  45.     if p then
  46.       if p < length number then
  47.  
  48.         // total up fraction portion in frac_total
  49.         f = number [p + 1 : TO_END] : -8 : '0'
  50.         frac_total = if (pos '-' number) then
  51.                        frac_total - f
  52.                      else
  53.                        frac_total + f
  54.                      end
  55.  
  56.         // negative fractional sum
  57.         if frac_total < 0 then
  58.           int_total = int_total - 1
  59.           frac_total = frac_total + 100000000
  60.  
  61.         // fractional overflow
  62.         elseif frac_total > 99999999 then
  63.           overflow = length frac_total - 8
  64.           int_total = int_total + frac_total [1 : overflow]
  65.           frac_total = frac_total [overflow + 1 : TO_END]
  66.         end
  67.       end
  68.       number = number [1 : p - 1]
  69.     end
  70.  
  71.     // add up integer portion
  72.     int_total = int_total + number
  73.  
  74.     // setup for next search
  75.     right width
  76.   else
  77.     break
  78.   end
  79. end
  80.  
  81. // restore cursor position and update display
  82. popcursor
  83. display
  84.  
  85. if frac_total then
  86.  
  87.   // compensate for negative integer sum
  88.   // and positive fraction sum
  89.   if int_total < 0 then
  90.     int_total = int_total + 1
  91.     frac_total = 100000000 - frac_total
  92.   end
  93.  
  94.   // right justify fraction and pad with zeros
  95.   frac_total = frac_total:8:'0'
  96.  
  97.   // remove trailing zeros and combine integer and
  98.   // fraction portion
  99.   int_total = int_total + '.' +
  100.                 frac_total [1 : posnot '0' frac_total 'r']
  101. end
  102.  
  103. if not int_total then
  104.   int_total = 0
  105. end
  106.  
  107. // display the sum and prompt the user to enter
  108. if (okbox  "Sum of " + count + " numbers is: " + int_total  + ".  Enter into text?" "Block Sum") == "Ok" then
  109.   write int_total
  110. end
  111.